package com.sh.rbac.controller;

import com.sh.rbac.core.common.annotation.SysLog;
import com.sh.rbac.domain.dto.dept.DeptQueryDTO;
import com.sh.rbac.domain.dto.dept.DeptSaveDTO;
import com.sh.rbac.domain.dto.dept.DeptUpdateDTO;
import com.sh.rbac.domain.entity.Dept;
import com.sh.rbac.core.common.enums.OperationTypeEnum;
import com.sh.rbac.domain.vo.dept.DeptVO;
import com.sh.rbac.core.common.result.Result;
import com.sh.rbac.service.DeptService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门控制器
 *
 * @author lucky
 * @since 2024-06-26
 */

@Tag(name = "部门管理", description = "部门相关操作")
@RequestMapping("sys/dept")
@RestController
public class DeptController {

    @Autowired
    private DeptService deptService;

    @Operation(summary = "查询部门列表")
    @ApiResponse(responseCode = "200", description = "返回查询结果", content = @Content(schema = @Schema(implementation = Dept.class)))
    @PostMapping("list")
    @PreAuthorize("@ps.hasPerm('sys:dept:list')")
    public Result<List<DeptVO>> getList(@RequestBody DeptQueryDTO deptQueryDTO) {
        return Result.success(deptService.getDeptList(deptQueryDTO));
    }


    @SysLog(module = "系统管理->部门管理", type = OperationTypeEnum.ADD)
    @Operation(summary = "添加部门", description = "保存一个新部门")
    @ApiResponse(responseCode = "200", description = "成功保存部门")
    @PostMapping("save")
    @PreAuthorize("@ps.hasPerm('sys:dept:save')")
    public Result<Boolean> save(@RequestBody DeptSaveDTO deptSaveDTO) {
        return deptService.save(deptSaveDTO) ? Result.success() : Result.failure();
    }


    @SysLog(module = "系统管理->部门管理", type = OperationTypeEnum.UPDATE)
    @Operation(summary = "更新部门", description = "根据ID更新部门信息")
    @ApiResponse(responseCode = "200", description = "成功更新部门")
    @PutMapping("update")
    @PreAuthorize("@ps.hasPerm('sys:dept:update')")
    public Result<Boolean> update(@RequestBody DeptUpdateDTO deptUpdateDTO) {
        deptService.checkDataScope(List.of(deptUpdateDTO.getId()));
        return deptService.updateById(deptUpdateDTO) ? Result.success() : Result.failure();
    }

    @SysLog(module = "系统管理->部门管理", type = OperationTypeEnum.DELETE)
    @Operation(summary = "删除部门", description = "根据ID删除部门信息")
    @ApiResponse(responseCode = "200", description = "成功删除部门")
    @DeleteMapping("delete/{id}")
    @PreAuthorize("@ps.hasPerm('sys:dept:delete')")
    public Result<Boolean> delete(@PathVariable Long id) {
        deptService.checkDataScope(List.of(id));
        return deptService.removeById(id) ? Result.success() : Result.failure();
    }

    @Operation(summary = "根据ID查询部门")
    @ApiResponse(responseCode = "200", description = "返回查询结果", content = @Content(schema = @Schema(implementation = Dept.class)))
    @GetMapping("get/{id}")
    @PreAuthorize("@ps.hasPerm('sys:dept:get')")
    public Result<DeptVO> get(@PathVariable Long id) {
        deptService.checkDataScope(List.of(id));
        return Result.success(deptService.getById(id));
    }


    @SysLog(module = "系统管理->部门管理", type = OperationTypeEnum.UPDATE)
    @Operation(summary = "更新部门状态", description = "根据ID更新部门状态")
    @ApiResponse(responseCode = "200", description = "成功更新部门状态")
    @PutMapping("update-status/{id}/{status}")
    @PreAuthorize("@ps.hasPerm('sys:dept:update-status')")
    public Result<Boolean> updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        deptService.checkDataScope(List.of(id));
        return deptService.updateStatus(id, status) ? Result.success() : Result.failure();
    }
}
